$(function () {
    page();
    load();
})

function page() {
    $("#tab_data").bootstrapTable({
        //请求方式  get请求中文会乱码，建议使用post请求
        method: 'post',
        //请求头部的设置
        contentType: 'application/json',
        //url
        url: '/Account.do/Page',
        //设置交替行样式
        striped: true,
        //初始显示页号
        pageNumber: 1,
        //设置表格高度
        //height: 350,
        //是否显示分页
        pagination: true,
        //设置是服务端分页还是客户端分页
        sidePagination: 'server',
        //页大小
        pageSize: 5,
        //分页列表
        pageList: [3, 5, 6, 10],
        //上传到服务端的参数
        queryParams: function (params) {
            let account = $("#accid").val();
            let rname = $("#rname").val();
            console.log(account + "\n" + rname);
            return {
                num: params.offset / params.limit + 1,
                size: params.limit,
                obj: {account: account, rname: rname}
            }
        },
        // 定义列名
        columns: [
            {
                title: "<input type='checkbox' id='checkbox'  />", field: "account", align: "center",
                formatter: function (vale, row, index) {
                    let img = "<input type='checkbox' style='width: 25px;height: 25px;' name='accid' value='" + vale + "'/>";
                    return img
                }
            },
            {
                title: "头像", field: "image", align: "center",
                formatter: function (vale, row, index) {
                    let img = "<img src='/image/" + vale + "' height='50px' width='50px' />";
                    return img
                }
            },
            {title: "帐号", field: "account", align: "center"},
            {title: "密码", field: "password", align: "center"},
            {title: "姓名", field: "rname", align: "center"},
            {
                title: "性别", field: "rsex", align: "center",
                formatter: function (vale, row, index) {
                    return vale == 0 ? "男" : "女"
                }
            },
            {title: "手机号码", field: "rphone", align: "center"},
            {title: "籍贯", field: "rorigo", align: "center"},
            {title: "专业", field: "rmajor", align: "center"},
            {title: "注册时间", field: "actime", align: "center"},
            {
                title: "操作", field: "account", align: "center",
                formatter: function (vale, row, index) {
                    let account = JSON.stringify(row);
                    let up_btn = "  <button class=\"btn btn-primary\"  data-toggle=\"modal\" data-target=\"#exampleModal\" onclick='update(" + account + ")'>修改</button>";
                    let del_btn = "  <button class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#delModal\"  onclick=\"LoadRoleDel('" + vale + "')\">删除</button>";

                    return up_btn + "" + del_btn
                }
            }
        ]
    })
    load();
}

function LoadRoleAdd() {
    //重置表单
    $("#AddUpDate")[0].reset();

    $("[name='account']").prop("readonly", false);
    let sex = " <input type=\"radio\" checked name=\"rsex\" value='0' style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "      <strong>男</strong> &nbsp;&nbsp;\n" +
        "      <input type=\"radio\" name=\"rsex\" value='1'  style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "       <strong>女</strong>";
    $("#sex").html(sex);
    $("[name='rpersonality']").text(null);
    let renglish = "<option value=\"-1\" >--请选择--</option>" +
        "          <option value=\"0\" >--不好--</option>" +
        "          <option value=\"1\" >--良好--</option>" +
        "           <option value=\"2\" >--优秀--</option> ";
    $("[name='renglish']").html(renglish);
    let rmarital = "<option value=\"-1\" >--请选择--</option>" +
        "          <option value=\"0\" >--未婚--</option>" +
        "           <option value=\"1\" >--已婚--</option> ";
    $("[name='rmarital']").html(rmarital);

    $("#exampleModalLabel").text("添加用户信息")
    $("#AddUpDate").prop("action", "/Account.do/Add?auths=adduser");

    submitForm();
}


function update(account) {
    //重置表单
    $("#AddUpDate")[0].reset();
    $("#img").prop("hidden", false);
    $("#img").prop("src", "/image/"+account.image);
    $("[name='account']").prop("readonly", true);

    $.each(account, function (key, value) {
        $("input[name=" + key + "]").val(value);
    })
    let sex = " <input type=\"radio\" " + (account.rsex == 0 ? 'checked' : '') + " name=\"rsex\" value='0' style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "      <strong>男</strong> &nbsp;&nbsp;\n" +
        "      <input type=\"radio\" " + (account.rsex == 1 ? 'checked' : '') + "  name=\"rsex\" value='1'  style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "       <strong>女</strong>";
    $("#sex").html(sex);
    $("[name='rpersonality']").text(account.rpersonality);
    let renglish = " <option " + (account.rpersonality == 0 ? 'selected' : '') + "  value=\"0\" >--不好--</option>" +
        "          <option " + (account.rpersonality == 1 ? 'selected' : '') + "  value=\"1\" >--良好--</option>" +
        "           <option " + (account.rpersonality == 2 ? 'selected' : '') + "  value=\"2\" >--优秀--</option> ";
    $("[name='renglish']").html(renglish);
    let rmarital = "<option " + (account.renglish == 0 ? 'selected' : '') + "  value=\"0\" >--未婚--</option>" +
        "           <option " + (account.renglish == 1 ? 'selected' : '') + "  value=\"1\" >--已婚--</option> ";
    $("[name='rmarital']").html(rmarital);

    $("#exampleModalLabel").text("修改用户信息")
    $("#AddUpDate").prop("action", "/Account.do/UpDate?auths=updateuser");

    submitForm();
}

function submitForm() {
    $("#exampleModal").modal("hide");
    $("#tab_data").bootstrapTable("refresh")
    $("#exampleModal").modal("show");
}

function LoadRoleDel(id) {
    $("#delModal").modal("show");
    if (id=="YYDS"){
        location.href = "/Butongguo.html";
    }else {
        $("#delSubmit").one("click", function () {
            $.post("/Account.do/Delete?auths=deluser", {account: id}, function (data) {
                $("#delModal").modal("hide");
                $("#tab_data").bootstrapTable("refresh")
            })
        })
    }
}

function accountrole() {
    var j = $("[name='accid']:checkbox:checked")

    if (j.length == 0 || j.length > 1) {
        alert("请选择一个账号设置角色！");
        location.href = "/account/Account.html";
    } else {
        for (var i = 0; i < j.length; i++) {
            if (j[i].value == "YYDS") {
                location.href = "/Butongguo.html";
                return;
            }
        }
        var id = j[0].value;
        $.post("/Accountrole.do/AccountRoleAll",{account:id},function (sdata) {
            console.log(sdata);
            $("[name='accid']").val(id);
            let str ="";
            $.each(sdata,function (index,item) {
                str +="<div class='row'><div class='col'>" +
                    " <input style='width: 25px;height: 25px;' type=\"checkbox\" "+(item.tf==true?'checked':'')+" id=\"checkbox\" value='"+item.roleid+"' name=\"roleid\">&nbsp;" +
                    " <input type=\"button\" id=\"button\" class=\"btn btn-secondary\" value=\""+item.rolename+"\" ><br>"
                    "</div></div>";
            });
            $("#roleid").html(str);
        })
        $("#AccRoleModal").modal("show");
    }

    $("#btn").on("click", function () {
        $.ajax({
            type: "post",
            url: "/Accountrole.do/DelAdd?auths=AccountRole",
            data:$("#AccRole").serialize(),
            success: function (data) {
                $("#AccRoleModal").modal("hide");
            }
        })
        load();
    })
}

// 刷新页面
function load() {
    $("#tab_data").bootstrapTable("refresh")
}

function accid() {
    let accid = $("#aid").val();
    $.post("/Account.do/Register",{account:accid},function (sdata) {
        if (sdata==-1){
            //重置表单
            $("#AddUpDate")[0].reset();
            $("#jg").prop("hidden",false);
        }
    })
}
function dian(){
    $("#jg").prop("hidden",true);
}

//    监听用户名的失去焦点事件
function callName(mythis) {
    $.get("/Account.do/changeChineseToPinyin",{username:$(mythis).val()},function (data) {
        //json字符  ，把json字符串转成 js对象
        //把值取出来，放入
        $("#aid").val(data.value)
    })

}